#pragma once
#include "faiss_index.h"
#include "httplib.h"
#include "index_factory.h"
#include "vector_database.h"
#include <rapidjson/document.h>
#include "raft_stuff.h"
class HttpServer{
public:
    enum class CheckType{
        SEARCH,
        INSERT,
        QUERY,
        UPSERT
    };

    HttpServer(const std::string& host,int port,VectorDatabase* vector_database_,RaftStuff* raft_stuff);
    void start();
private:
    void searchHandler(const httplib::Request &req, httplib::Response &res);
    void insertHandler(const httplib::Request &req, httplib::Response &res);
    void upsertHandler(const httplib::Request &req, httplib::Response &res);
    void queryHandler(const httplib::Request &req, httplib::Response &res);
    void snapshotHandler(const httplib::Request &req, httplib::Response &res);
    void setLeaderHandler(const httplib::Request &req, httplib::Response &res);
    void addFollowerHandler(const httplib::Request &req, httplib::Response &res);
    void listNodeHandler(const httplib::Request &req, httplib::Response &res);
    void getNodeHandler(const httplib::Request &req, httplib::Response &res);
    
    void setJsonResponse(httplib::Response &res,const rapidjson::Document &json_response);
    void setErrorJsonResponse(httplib::Response &res,int errCode,const std::string &error_message);

    bool isRequestValid(const rapidjson::Document& json_request, CheckType check_type);

    IndexFactory::IndexType getIndexTypeFromRequest(const rapidjson::Document &req);
    httplib::Server server;
    std::string host;
    int port;
    VectorDatabase* vector_database;
    RaftStuff* raft_stuff_;
};